﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebCalculator
{
    public partial class Calc : System.Web.UI.Page
    {
        //string firstNum = "";
        //string op = "";
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn7_Click(object sender, EventArgs e)
        {
            lblResult.Text += "7";

            /*lblResult.Text = lblResult.Text + "7"; */
        }

        protected void btn8_Click(object sender, EventArgs e)
        {
            lblResult.Text += "8";
        }

        protected void btn9_Click(object sender, EventArgs e)
        {
            lblResult.Text += "9";
        }

        private void GetOperator(string op)
        {
            Session["firstNum"] = lblResult.Text;
            Session["op"] = op;
            lblResult.Text = "";

        }
        protected void btnDivide_Click(object sender, EventArgs e)
        {
            GetOperator("/");
        }

        protected void btnPercent_Click(object sender, EventArgs e)
        {
            GetOperator("%");

        }

        protected void btn4_Click(object sender, EventArgs e)
        {
            lblResult.Text += "4";
        }

        protected void btn5_Click(object sender, EventArgs e)
        {
            lblResult.Text += "5";
        }

        protected void btn6_Click(object sender, EventArgs e)
        {
            lblResult.Text += "6";
        }

        protected void btnMultiply_Click(object sender, EventArgs e)
        {
            GetOperator("*");
        }

        protected void btnInvert_Click(object sender, EventArgs e)
        {
            lblResult.Text = (1 / double.Parse(lblResult.Text)).ToString();
        }

        protected void btn1_Click(object sender, EventArgs e)
        {
            lblResult.Text += "1";
        }

        protected void btn2_Click(object sender, EventArgs e)
        {
            lblResult.Text += "2";
        }

        protected void btn3_Click(object sender, EventArgs e)
        {
            lblResult.Text += "3";
        }

        protected void btnSubtract_Click(object sender, EventArgs e)
        {
            GetOperator("-");
        }

        protected void btnEquals_Click(object sender, EventArgs e)
        {
            double firstNumber;
            double secondNumber;
            string op = Session["op"].ToString ();
            firstNumber = double.Parse(Session["firstNum"].ToString ());
            secondNumber = double.Parse(lblResult.Text);
            //firstNumber = double.Parse(hdnFirstNum.Value);

            switch (op)
            {
                case "+":
                    {
                        lblResult.Text = (firstNumber + secondNumber).ToString();
                        break;
                    }

                case "-":
                    {
                        lblResult.Text = (firstNumber - secondNumber).ToString();
                        break;
                    }
                case "*":
                    {
                        lblResult.Text = (firstNumber * secondNumber).ToString();
                        break;
                    }
                case "/":
                    {
                        lblResult.Text = (firstNumber / secondNumber).ToString();
                        break;
                    }
                case "%":
                    {
                        lblResult.Text = ((firstNumber / secondNumber) * 100).ToString();
                        break;
                    }
            }
            
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            GetOperator("+");
        }

        protected void btnDecimal_Click(object sender, EventArgs e)
        {
            lblResult.Text += ".";
        }

        protected void btn0_Click(object sender, EventArgs e)
        {
            lblResult.Text += "0";
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            lblResult.Text = "";
            Session["firstNum"] = "";
            Session["op"] = "";
        }
    }
}